//
//  MBProgressHUD+MZAdd.m
//  TF02
//
//  Created by Mokyz on 16/5/31.
//  Copyright © 2016年 Moky. All rights reserved.
//

#import "MBProgressHUD+MZAdd.h"

@implementation MBProgressHUD (MZAdd)

+(instancetype)sharedInstanceWithView:(UIView *)view
{
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    
    // 改变背景颜色
    hud.bezelView.backgroundColor = [UIColor blackColor];
    // 改变文字颜色
    hud.contentColor = [UIColor whiteColor];
    
    [view addSubview:hud];
    
    hud.offset = CGPointMake(0.f, 0.f);
    
    hud.animationType = MBProgressHUDAnimationZoomIn;
    
    hud.userInteractionEnabled = NO;
    
    return hud;
}

+ (void)showIndicatorWithText:(NSString *)text toView:(UIView *)view
{    
    MBProgressHUD *hud = [self sharedInstanceWithView:view];
    [hud showAnimated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = text;
    hud.userInteractionEnabled = YES;
    hud.minSize = CGSizeMake(100.f, 100.f);
}

+ (void)showText:(NSString *)text toView:(UIView *)view
{
    MBProgressHUD *hud = [self sharedInstanceWithView:view];
    
    [hud showAnimated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = text;
    [hud hideAnimated:YES afterDelay:2.f];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [hud removeFromSuperview];
    });
}

+ (void)showSuccessViewWithText:(NSString *)text toView:(UIView *)view
{
    [self showCustomView:@"Success" withText:text toView:view];
}

+ (void)showFailureViewWithText:(NSString *)text toView:(UIView *)view
{
    [self showCustomView:@"Failure" withText:text toView:view];
}

+ (void)showCustomView:(NSString *)imageName withText:(NSString *)text toView:(UIView *)view
{
    MBProgressHUD *hud = [self sharedInstanceWithView:view];
    [hud showAnimated:YES];
    UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    hud.customView = imageView;
    hud.mode = MBProgressHUDModeCustomView;
    hud.label.text = text;
    
    [hud hideAnimated:YES afterDelay:1.f];
}

+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    [self hideHUDForView:view animated:YES];
}

+ (void)hide
{
    [self hideHUDForView:nil];
}
@end
